import java.util.Arrays;

public class Test {


    public static void main(String[] args) {
        int[] arr = {5,1,3,5,10,7,4,2,8};
        minSubArrayLen(15,arr);
    }

    /**
     * 三数之和
     */
    public int triangleNumber(int[] nums) {
        //1.排序
        Arrays.sort(nums);
        int count = 0;
        //2.固定一个数
        for(int i = nums.length-1;i>=2;i--) {
            //3.双指针判断三角形
            int left = 0;
            int right = i-1;
            while(left < right) {
                if(nums[left]+nums[right] > nums[i]) {
                    count+=(right-left);
                    right--;
                }else {
                    left++;
                }
            }
        }
        return count;
    }
    //滑动窗口，长度最小的子数组
    public static int minSubArrayLen(int target, int[] nums) {
        int n = nums.length;
        int left = 0;
        int right = 0;
        int len = 0;
        int sum = 0;
        int min = 0;
        while(right < n) {
            sum+=nums[right];
            while(sum >= target) {
                len = right-left+1;
                if(len <= min)
                     min = len;
                left++;
                sum = sum - nums[left-1];
            }
            right++;
        }
        return len;
    }
}
